package com.hyt.it.ogt.ykcj.service.exchange.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hyt.core.util.StringUtils;
import com.hyt.it.ogt.ykcj.common.enums.CommonEnums;
import com.hyt.it.ogt.ykcj.domain.exchange.ApiLog;
import com.hyt.it.ogt.ykcj.domain.exchange.ExchangeCandidate;
import com.hyt.it.ogt.ykcj.domain.exchange.MessageInfo;
import com.hyt.it.ogt.ykcj.domain.param.ApiLogQueryParam;
import com.hyt.it.ogt.ykcj.domain.vo.ExamExamineeSubjectVo;
import com.hyt.it.ogt.ykcj.mapper.exchange.ApiLogMapper;
import com.hyt.it.ogt.ykcj.service.exchange.IApiLogService;
import com.hyt.it.ogt.ykcj.service.exchange.IExchangeCandidateService;
import com.hyt.it.ogt.ykcj.service.grade.IEnterpriseExamineeInfoService;
import com.hyt.it.ogt.ykcj.service.grade.IEnterpriseGroupExamineeInfoService;

import lombok.extern.slf4j.Slf4j;

/**
 * description
 * 同步数据日志记录
 *
 * @author yaojian
 * @createTime 2021/12/24
 */
@Slf4j
@Service
public class ApiLogServiceImpl extends ServiceImpl<ApiLogMapper, ApiLog> implements IApiLogService {

    @Resource
    ApiLogMapper apiLogMapper;
    @Resource
    private IExchangeCandidateService exchangeCandidateService;
    @Resource
    private IEnterpriseExamineeInfoService enterpriseExamineeInfoService;
    @Resource
    private IEnterpriseGroupExamineeInfoService enterpriseGroupExamineeInfoService;

    @Override
    public List<ApiLog> selectApiLogList(ApiLogQueryParam apiLog, int pageNo, int pageSize) {
        log.debug("selectApiLogList params:{}", JSON.toJSON(apiLog));
        PageInfo<ApiLog> pageInfo = PageHelper.startPage(pageNo, pageSize).doSelectPageInfo(() -> {
            ApiLogQueryParam paremApiLog = new ApiLogQueryParam();
            paremApiLog.setStatus(apiLog.getStatus());
            paremApiLog.setQueryStartTime(apiLog.getQueryStartTime());
            paremApiLog.setQueryEndTime(apiLog.getQueryEndTime());
            apiLogMapper.selectApiLogList(paremApiLog);
        });
        return pageInfo.getList();
    }

    @Override
    public int insertApiLog(ApiLog apiLog) {
        return apiLogMapper.insertApiLog(apiLog);
    }

    @Override
    public void saveCandidateInfo(MessageInfo messageInfo) {
        // 添加保存日志
        ApiLog apiLog = new ApiLog();
        apiLog.setCreateTime(new Date());
        apiLog.setApiUrl("/saveCandidateInfo");
        apiLog.setServerName(CommonEnums.EXCHANGE_EXAMINEE.getCode());
        apiLog.setParam(messageInfo.getMsg());
        apiLog.setStatus(CommonEnums.EXCHANGE_STATUS_UNSAVE.getCode());
        this.insertApiLog(apiLog);
        // 异步执行的主要目的是想让数据落库，如果数据还没有落库就被消费了，就是一个异常数据
        saveData(apiLog);
        log.info("接收考试的数据对象json内容落库成功");
    }


    /**
     * 保存成绩数据
     *
     * @param apiLog
     * @return
     */
    private void saveData(ApiLog apiLog) {
        try {
            // 把消息字符串转成对象
            ExchangeCandidate exchangeCandidate = JSON.parseObject(apiLog.getParam(), ExchangeCandidate.class);
            saveCandidateInfoForYunkaoData(apiLog, exchangeCandidate);
            apiLogMapper.updateById(apiLog);
            apiLogMapper.updateById(apiLog);
        } catch (Exception e) {
            log.error("#处理ApiLog数据异常，ApiID:{}", apiLog.getId(), e);
            ApiLog apiLogError = new ApiLog();
            apiLogError.setId(apiLog.getId());
            apiLogError.setStatus(CommonEnums.EXCHANGE_STATUS_ERROR.getCode());
            apiLogMapper.updateById(apiLogError);
        }
    }


    private void saveCandidateInfoForYunkaoData(ApiLog apiLog, ExchangeCandidate exchangeCandidate) {
        log.info("##执行saveCandidateInfoForYunkaoData开始!!!!:{}", apiLog.getId());
        ExamExamineeSubjectVo examExamineeSubjectVo = exchangeCandidateService.saveCandidateInfo(exchangeCandidate);
        log.info("##执行saveCandidateInfoForYunkaoData结束!!!!");
        // 更新保存日志
        if (examExamineeSubjectVo != null && StringUtils.isNotEmpty(examExamineeSubjectVo.getExamId())) {
            // 重新计算考试合格标识
            enterpriseExamineeInfoService.refreshEnterpriseExamineeQualified(examExamineeSubjectVo.getExamId(), examExamineeSubjectVo.getExamineeId());
            enterpriseGroupExamineeInfoService.refreshEnterpriseExamineeQualified(examExamineeSubjectVo.getExamId(), examExamineeSubjectVo.getExamineeId());
            apiLog.setStatus(CommonEnums.EXCHANGE_STATUS_SAVE.getCode());
        } else {
            apiLog.setStatus(CommonEnums.EXCHANGE_STATUS_SAVE_PART.getCode());
        }
    }
}
